package cn.geminis.auth.server.service;

import cn.geminis.resources.core.client.UserClient;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

/**
 * @author puddi
 */
@Service
@AllArgsConstructor
public class GeminiAuthenticationManager implements AuthenticationManager {

    private final UserClient userClient;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        var usernamePasswordAuthentication = (UsernamePasswordAuthenticationToken) authentication;
        this.userClient.checkPassword(
                usernamePasswordAuthentication.getName(),
                usernamePasswordAuthentication.getCredentials().toString()
        );
        return authentication;
    }

}
